package com.zlr.hiring.company.dao;

import java.util.ArrayList;
import java.util.List;

import com.zlr.hiring.common.util.DBUtil;
import com.zlr.hiring.company.entity.PositionEntity;

public class PositionDao {
	/**
	 * 发布职位
	 */
	public int add(PositionEntity position) {
		String sql = "INSERT INTO position VALUES(NULL ,?,?,?,?,?,?,NOW())";
		List<Object> params = new ArrayList<>();
		params.add(position.getCompanyId());
		params.add(position.getName());
		params.add(position.getDuty());
		params.add(position.getAbility());
		params.add(position.getSalary1());
		params.add(position.getSalary2());
		int row = DBUtil.executeUpdate(sql, params);
		return row;
	}

	/**
	 * 查询所有
	 */
	public List<PositionEntity> list() {
		String sql = "SELECT c.name as 'company$name', c.address as 'company$address', c.slogan as 'company$slogan', p.id, p.name, p.salary1, p.salary2, p.release_time FROM company c, position p WHERE c.id=p.company_id ORDER BY p.id DESC";
		List<PositionEntity> positionList = DBUtil.executeQuery(
				PositionEntity.class, sql);
		return positionList;
	}

	public PositionEntity detail(int id) {
		StringBuffer sb = new StringBuffer();
		sb.append("SELECT ");
		sb.append("c.name as 'company$name', ");
		sb.append("c.email as 'company$email', ");
		sb.append("c.phone as 'company$phone', ");
		sb.append("c.linkman as 'company$linkman', ");
		sb.append("c.address as 'company$address', ");
		sb.append("c.numbers as 'company$numbers', ");
		sb.append("c.slogan as 'company$slogan', ");
		sb.append("p.id, ");
		sb.append("p.name, ");
		sb.append("p.duty, ");
		sb.append("p.ability, ");
		sb.append("p.salary1, ");
		sb.append("p.salary2, ");
		sb.append("p.release_time ");
		sb.append("FROM company c, position p ");
		sb.append("WHERE c.id=p.company_id AND p.id=? ");
		return DBUtil.getUniqueResult(PositionEntity.class, sb.toString(), id);
	}

	/**
	 * 查询企业发布的所有职位讯息
	 */
	public List<PositionEntity> listById(int companyId) {
		List<PositionEntity> positionList = DBUtil.executeQuery(
				PositionEntity.class,
				"SELECT * from position where company_id=? order by ID DESC",
				companyId);
		return positionList;
	}

	/**
	 * 删除
	 */
	public int delete(int id) {
		return DBUtil.executeUpdate("DELETE from position where id=?", id);
	}

	/**
	 * 职位详细信息查询
	 */

	public PositionEntity get(int id) {
		String sql = "select * from position where id=?";
		return DBUtil.getUniqueResult(PositionEntity.class, sql, id);

	}

	/**
	 * 修改
	 */

}
